#include <bits/stdc++.h>
using namespace std;

int main()
{
  // 请在此输入您的代码
  int n;
  cin>>n;
  int arr[n];
  long long s[n]={0};
  for(int i=0;i<n;i++)
  {
      cin>>arr[i];
      if(i==0)
      {
          s[0]=arr[0];
      }
      else
      {
          s[i]=arr[i]+s[i-1];
      }
  }
  long long ret=0x3f3f3f3f3f3f3f3f;
  for(int r1=0;r1<n;r1++)
  {
      for(int l2=r1+1;l2<n;l2++)
      {
          int l1=r1,r2=l2;
          while(l1>=0  &&r2<=n-1)
          {

              if(s[r1]-s[l1]+arr[l1] >s[r2]-s[l2]+arr[l2])
              {
                  ret=min(ret,s[r1]-s[l1]+arr[l1] -(s[r2]-s[l2]+arr[l2]));
                  r2++;
              }
              else
              {
                  ret=min(ret,s[r2]-s[l2]+arr[l2]-(s[r1]-s[l1]+arr[l1]));
                  l1--;

              }
          }
      }
  }
  cout<<ret;
  
  return 0;
}